﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class AssembleDetails : BaseRepository<AssembleDetailsInfo>, IAssembleDetails 
    { 
        private const string PARM_ASSEMBLEDETAILS_PRODUCTSN = "@ProductSN";

        private const string PARM_ASSEMBLEDETAILS_MO = "@MOID";
        private const string PARM_ASSEMBLEDETAILS_IPADDR = "@IPAddr";

        public IList<AssembleDetailsInfo> GetAssembleDetailsbySN(string productSN)
        {
            SqlParameter parm = GetParameter();
            //Bind the parameter
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("AssembleDetails_selectbySN", parm);
        }

        public IList<AssembleDetailsInfo> GetAssembleDetailsbyIPMO(string mo, string ip)
        {
            SqlParameter[] parms = GetIPMOParameters();
            parms[0].Value = mo;
            parms[1].Value = ip;
            IList<AssembleDetailsInfo> Entities = new List<AssembleDetailsInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "AssembleDetailOnlineItem_selectbyIPandMO", parms))
            {
                while (rdr.Read())
                {
                    AssembleDetailsInfo Entity = new AssembleDetailsInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetValue(2).ToString(), rdr.GetInt32(3));
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }


        public SqlParameter[] GetIPMOParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_ASSEMBLEDETAILS_MO, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_ASSEMBLEDETAILS_IPADDR,SqlDbType.NVarChar,20)
                };
            return parms;
        }


        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override AssembleDetailsInfo GetEntityRecord(SqlDataReader rdr)
        {
            AssembleDetailsInfo pro = new AssembleDetailsInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetValue(2).ToString());
            return pro;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_ASSEMBLEDETAILS_PRODUCTSN, SqlDbType.NVarChar, 50);
            return Parm;
        }
    }
}
